package com.changgou.goods.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Spu;
import com.changgou.goods.service.SpuService;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping("/spu")
public class SpuController {


    @Autowired
    private SpuService spuService;


    /**
     * @param goods
     * @return com.changgou.entity.Result
     * @description: spu 和 sku 的添加
     * @author 大佬味的小男孩
     * @date 2020/07/19 18:20
     */
    @PostMapping("add")
    public Result add(@RequestBody Goods goods) {
        spuService.add(goods);
        return new Result(true, StatusCode.OK, "添加成功");
    }


    /**
     * @param id
     * @return com.changgou.entity.Result
     * @description: 根据ID查询 SPU和SKU列表
     * @author 大佬味的小男孩
     * @date 2020/07/19 22:24
     */
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable String id) {
        Goods goods = spuService.findGoodsById(id);
        return new Result(true, StatusCode.OK, "查询成功", goods);
    }

    /**
     * @param goods
     * @return com.changgou.entity.Result
     * @description: 保存修改
     * @author 大佬味的小男孩
     * @date 2020/07/19 22:48
     */
    @PutMapping("update/{id}")
    public Result update(@RequestBody Goods goods) {
        spuService.update(goods);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * @param id
     * @return com.changgou.entity.Result
     * @description: 审核spu商品并自动上下架
     * @author 大佬味的小男孩
     * @date 2020/07/20 12:56
     */
    @PutMapping("audit/{id}")
    public Result audit(@PathVariable String id) {
        spuService.audit(id);
        return new Result(true, StatusCode.OK, "审核商品并上架成功");
    }

    /**
     * @param id
     * @return com.changgou.entity.Result
     * @description: 商品下架
     * @author 大佬味的小男孩
     * @date 2020/07/20 16:22
     */
    @PutMapping("pull/{id}")
    public Result pull(@PathVariable String id) {
        spuService.pull(id);
        return new Result(true, StatusCode.OK, "商品下架成功");
    }

    /**
     * @param id
     * @return com.changgou.entity.Result
     * @description: 逻辑删除
     * @author 大佬味的小男孩
     * @date 2020/07/20 20:21
     */
    @PutMapping("delete/{id}")
    public Result detele(@PathVariable String id) {
        spuService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }


    /**
     * @param id
     * @return com.changgou.entity.Result
     * @description: 恢复商品
     * @author 大佬味的小男孩
     * @date 2020/07/20 20:23
     */
    @PutMapping("reduction/{id}")
    public Result reduction(@PathVariable String id) {
        spuService.reduction(id);
        return new Result(true, StatusCode.OK, "商品还原成功");
    }


    /**
     * @param id
     * @return com.changgou.entity.Result
     * @description: 物理删除 商品彻底删除
     * @author 大佬味的小男孩
     * @date 2020/07/20 20:24
     */
    @PutMapping("deleteTrue/{id}")
    public Result deleteTrue(@PathVariable String id) {
        spuService.deleteTrue(id);
        return new Result(true, StatusCode.OK, "商品彻底删除成功");
    }


    /**
     * @param id
     * @return com.changgou.entity.Result<com.changgou.goods.pojo.Spu>
     * @description:  根据spuid 查询spu数据
     * @author 大佬味的小男孩
     * @date 2020/07/27 21:47
    */
    @GetMapping("/findSpuById/{id}")
    public Result<Spu> findSpuById(@PathVariable("id") String id){
        Spu spu = spuService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",spu);
    }

}
